/*******************************************************************************

[Last updated: June 4th, 2024]

This script outlines the workflow for each component script in the 
admin analysis pipeline. It should be executed before running any component script.

The script is organized as follows:

	Part A: Set up Stata Workspace
	Part B1 and B2: Define file paths and parameters for data building
	Part C: Execute scripts to build the data
	Part D: Execute scripts to analyze the data
	
Each subscript includes detailed information about its specific purpose.

*******************************************************************************/


* Part A: Setting up the work space --------------------------------------------

	* Preparing Stata's workspace
	clear all
	set maxvar 30000 

	* Parameter for updates
	global data_refresh		"Yes"
	global update			"Yes"
	global individual_run 	"Yes"

	
	
* Part B1: Establish path directory, for the pipeline overall -------------------
		
	* Functions, table shells, table notes
		global functions	"${FilePath2}/function/function_admin__functions.do"
		global shells		"${FilePath2}/function/function_admin__shells.do"
		global notes		"${FilePath2}/function/function_admin__notes.do"
		
		do "$functions"
		do "$shells"			
		do "$notes"
		
	* Folder to store Latex files
		capture: mkdir "${FilePath2}/reports"
		gl report_folder "${FilePath2}/reports"
		

		
* Part B2: Set-up for the data cleaning ----------------------------------------

	* Data sources

		* Source 1: programmatic data: Randomziation roster and demographics
		gl master 		${FilePath3}	// Randomization status of 2,070 officers
		gl demo 		${FilePath4}	// Demographic data of 2,070 officers
	
		* Source 2: Attendance: contain the post-period traininda dates
		gl att_file 	${FilePath5}
	
		* Source 3: Administrative outcomes from CPD
		gl admin_data	${FilePath4}	
	
		* Source 4: Census data aggregated at district level
		gl census		${FilePath5}
		
		* Source 5: Endline assessment data
		gl out_pre		${FilePath9}\assessment_pre_20210715								
		gl out_pos		${FilePath9}\assessment_pos_20210715								
		gl out_rub		${FilePath9}\assessment_rub_20210715
		
		
	* Data Build Parameters
	
		* Specifications
		gl spec	S5_60610				
		gl officer_mul 1
		
		* Full data period from Jan 2018 tp Feb 2022
		gl prestart		201801			
		gl postend		202202

		* Location to save the consolidated data file
		gl output1			${FilePath-Output1}
		gl interim_data		${FilePath-Output2}
	
	* Componental scrips
	
		gl pre_script	"${FilePath2}/build/build_2_data_cleaning_sub_pre.do"
		gl post_script	"${FilePath2}/build/build_2_data_cleaning_sub_post.do"
		
		
		
* Part C: Data preparation -----------------------------------------------------

if "$data_refresh" == "Yes" {
	
	* (A1) Combine all data
	do "${FilePath2}/build/build_1_data_consolidation.do"
	
	* (A2-D) Build the data set for analysis (This is an important file)
	do "${FilePath2}/build/build_2_data_cleaning_main.do"
	
}



* Part D: Analysis -------------------------------------------------------------

if "$update" == "Yes" {
	
	* Main Tables in the paper
	// Note: These scripts are dependent sequentially
	
		* Run regression on main outcomes (and some outcomes in the appendix)
		do  "${FilePath2}/analysis/analysis_1_all_regressions.do"
		
		* Write to latex tables
		do  "${FilePath2}/analysis/analysis_2_latex.do"
		
		* Make Plots
		do  "${FilePath2}/analysis/analysis_3_plot_latex.do"
		
	* Tables in Appendix A and B
	// Note these scripts are independent
		
		* Arrests
		do  "${FilePath2}/analysis/analysis_4_arrests_detailed.do"
		
		* Balance table, Attrition, Unit-Switch
		do  "${FilePath2}/analysis/analysis_5_1_balance.do"
		do  "${FilePath2}/analysis/analysis_5_2_attrition.do"
		do  "${FilePath2}/analysis/analysis_5_3_unit_switch.do"
		
		* Crime Rates
		do  "${FilePath2}/analysis/analysis_5_4_crime_rates.do"
		
		* Spill Over
		do  "${FilePath2}/analysis/analysis_5_5_spill_overs.do"
		
		* Alternative specification: DID
		do  "${FilePath2}/analysis/analysis_5_6_DID.do"
		
		* Alternative specification: Stacked data
		do  "${FilePath2}/analysis/analysis_5_7_alternative.do"

}


* END OF SCRIPT ----------------------------------------------------------------


